package com.yunhe.rentcars.pro.service.system.impl;

import com.yunhe.rentcars.commons.dto.Pageable;
import com.yunhe.rentcars.pro.dao.system.SysLogDao;
import com.yunhe.rentcars.pro.entity.system.SysLog;
import com.yunhe.rentcars.pro.service.system.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class SysLogServiceImpl implements SysLogService {
    @Autowired
    private SysLogDao sysLogDao;

    @InitBinder
    public void initBinder(ServletRequestDataBinder binder){
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd "),
                true));
    }

    @Override
    public void save(SysLog sysLog) {
        sysLogDao.save(sysLog);

    }

    @Override
    public Pageable<SysLog> findPage(SysLog sysLog, Integer offset, Integer limit, Date startTime,Date endTime) {
        Pageable<SysLog> pageable = new Pageable<>();
        List<SysLog> rows = new ArrayList<>();

        Long count = sysLogDao.findCount(sysLog,startTime,endTime);
        if(count > 0){
            rows = sysLogDao.findList(sysLog,offset,limit,startTime,endTime);
        }
        pageable.setTotal(count.intValue());
        pageable.setRows(rows);
        return pageable;
    }
}
